﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using StudentManagement.Interfaces;
using StudentManagement.SearchTypes;

namespace StudentManagement
{
    public class SearchTypeFactory
    {
        private static readonly Object _lock = new Object();
        private SearchTypeFactory()
        {
            RegisterSearchType(SearchType.Name, typeof(NameSearch));
            RegisterSearchType(SearchType.Birthday, typeof(BirthdaySearch));
            RegisterSearchType(SearchType.Class, typeof(ClassSearch));
            RegisterSearchType(SearchType.Address, typeof(AddressSearch));
        }

        private static SearchTypeFactory Instance;

        public static SearchTypeFactory GetInstance()
        {
            lock (_lock)
            {
                if (Instance == null)
                {
                    Instance = new SearchTypeFactory();
                }
                return Instance;
            }
        }

        private Hashtable ListSearchType = new Hashtable();
        
        public void RegisterSearchType(SearchType searchType, Type type)
        {
            if (ListSearchType[searchType] == null)
            {
                ListSearchType.Add(searchType, type);
            }
        }

        public ISearchType GetSearchType(SearchType searchType)
        {
            if (ListSearchType[searchType] == null)
            {
                return null;
            }
           return (ISearchType)Activator.CreateInstance(ListSearchType[searchType] as Type);
        }
    }
}
